layui.use(['form','layer','jquery','table'],function() {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table;

    table.on('toolbar(table)', function(obj){
        tableToolbarEnvent(obj);
        switch(obj.event){
            case 'addD':
                var index = layer.open({
                    type: 2
                    ,title: '新增'
                    ,area: ['50%', '100%']
                    ,content: '/manage/customer/addHtml/'
                    ,btn: ['保存', '取消']
                    ,offset: 'r'
                    ,yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                        var data = iframeWindow.getFormDataAndValidate();
                        if (!data[0]) {
                            return;
                        }

                        var master = data[1];
                        var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                        $.post("/manage/customer/add", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                            if (d.code == '200') {
                                top.layer.alert("新增成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.msg("新增失败," + d.msg, {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                            layer.close(index);
                        }, "json")
                    }
                });
                break;
            case 'updD':
                let tableCheckData = table.checkStatus(obj.config.id);
                let data = tableCheckData.data;
                if (data.length == 0) {
                    layer.msg("请选择要修改的数据");
                    return;
                }
                ;
                if (data.length != 1) {
                    layer.msg("请选择单条数据");
                    return;
                }
                ;
                var index = layer.open({
                    type: 2
                    ,title: '修改'
                    ,area: ['50%', '100%']
                    ,content: '/manage/common/editHtml/' + object.url + '/' + data[0].id
                    ,btn: ['保存', '取消']
                    ,offset: 'r'
                    ,yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                        var d = iframeWindow.getFormDataAndValidate();
                        if (!d[0]) {
                            return;
                        }
                        var master = d[1];
                        master.id = data[0].id;

                        var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                        $.post("/manage/customer/update", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                            if (d.code == '200') {
                                top.layer.msg("修改成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.msg("修改失败," + d.msg, {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                            layer.close(index);
                        }, "json")
                    }
                });
                break;
        };
    });
})